
package com.cari.baju.yog.utils;

import java.text.DecimalFormat;

import android.content.Context;
import android.graphics.Typeface;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.cari.baju.yog.BuildConfig;
import com.google.android.gms.maps.model.LatLng;

public class Utils {

    // font asset
    private static final String FNT_STROBERY = "font/stroberi.ttf";

    public static void TRACE(String tag, String msg) {
        if (BuildConfig.DEBUG)
            Log.d(tag, msg);
    }

    /**
     * Checking internet connection
     * 
     * @param context Context
     * @return boolean
     */
    public static boolean isConnectedToInternet(Context context)
    {
        ConnectivityManager conn = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conn != null)
        {
            NetworkInfo[] info = conn.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED)
                        return true;
        }
        return false;
    }

    /**
     * nilai jarak hanya diberi 1 angka dibelakang koma
     * 
     * @param d nilai yang akan di format
     * @return hasil format
     */
    public static double getRoundTwoDecimal(double d)
    {
        DecimalFormat twoDForm = new DecimalFormat("#.#");
        return Double.valueOf(twoDForm.format(d));
    }

    public static double hitungJarak(LatLng posUser, LatLng posLocation)
    {
        float[] result = new float[1];
        Location.distanceBetween(posUser.latitude, posUser.longitude, posLocation.latitude,
                posLocation.longitude, result);

        return result[0];
    }

    /**
     * set font
     * @param context
     * @param fontType
     * @return
     */
    public static Typeface getCustomFont(Context context) {
        return Typeface.createFromAsset(context.getAssets(), FNT_STROBERY);
    }
}
